package cn.xny.txcc.mq;


import cn.xny.txcc.domain.MessageStation;
import cn.xny.txcc.dto.StationDto;
import cn.xny.txcc.service.IMessageStationService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Date;


@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "service-consumer-station",
        topic = "Station-topic",
        selectorExpression = "course-sms",
        messageModel = MessageModel.CLUSTERING
)
public class StationConsumer implements RocketMQListener<String> {
    
    @Autowired
    private IMessageStationService MessageStationService;

    @Override
    public void onMessage(String s) {

        StationDto stationDto = JSON.parseObject(s, StationDto.class);
        stationDto.getUserIds().forEach(userId ->{
            MessageStation messageStation = new MessageStation();

            BeanUtils.copyProperties(stationDto, messageStation);
            messageStation.setIsread(0);
            messageStation.setSendTime(new Date());
            messageStation.setUserId(userId);
            MessageStationService.insert(messageStation);


        });

    }
}
